﻿/*!
* jQuery tree plugin: 
* 
* version 0.1 2011-03-10
* Requires jQuery v1.4.2 or later
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
* Authors: Kim Seoung Min
* Company : Kongyoung DB
*/

(function ($) {
    $.fn.Superlevelcombo = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }
    };

    var methods = {
        init: function (url) {

            var _url = url;
            var SuperLevel = $(this);

            $(this).click(function (e) {

                $('.AutoLevelDiv').remove();
                //$(document.body).append("<div class='AutoLevelDiv'>" + SuperLevel.val() + "</div>");
                $(document.body).append("<div class='AutoLevelDiv'></div>");
                $('.AutoLevelDiv').css("top", SuperLevel.offset().top + SuperLevel.outerHeight());
                $('.AutoLevelDiv').css("left", SuperLevel.offset().left);
                $('.AutoLevelDiv').css("width", SuperLevel.outerWidth());

                $('.AutoLevelDiv').append("<select id='AutoLevelCombo1'><option></option></select>&nbsp;");
                $('.AutoLevelDiv').append("<select id='AutoLevelCombo2'><option></option></select>&nbsp;");
                $('.AutoLevelDiv').append("<select id='AutoLevelCombo3'><option></option></select>&nbsp;&nbsp;");

                //$('.AutoLevelDiv').append("<div class='AutoLevelDivClose ui-widget-content'>닫기</div>");
                //$('.AutoLevelDivClose').click(function (e) {
                //    $('.AutoLevelDiv').remove();

                $('.AutoLevelDiv').append("<input type='button' class='AutoLevelBtnClose ui-widget-content' value='닫기' />");
                $('.AutoLevelBtnClose').click(function (e) {
                    $('.AutoLevelDiv').remove();
                });

                // 대분류선택 초기화
                fn_ProductZIPDown(_url, $('#AutoLevelCombo1'), $('#AutoLevelCombo1').val(), $('#AutoLevelCombo2').val(), $('#AutoLevelCombo3').val(), '');

                $('#AutoLevelCombo1').change(function (e) {
                    $('#AutoLevelCombo2, #AutoLevelCombo3').val("");

                    fn_ProductZIPDown(_url, $('#AutoLevelCombo2'), $('#AutoLevelCombo1').val(), $('#AutoLevelCombo2').val(), $('#AutoLevelCombo3').val(), '');

                    $('#AutoLevelCombo3').val("");

                    fn_ProductZIPDown(_url, $('#AutoLevelCombo3'), $('#AutoLevelCombo1').val(), $('#AutoLevelCombo2').val(), $('#AutoLevelCombo3').val(), '');
                });

                $('#AutoLevelCombo2').change(function (e) {
                    $('#AutoLevelCombo3').val("");
                    fn_ProductZIPDown(_url, $('#AutoLevelCombo3'), $('#AutoLevelCombo1').val(), $('#AutoLevelCombo2').val(), $('#AutoLevelCombo3').val(), '');
                });
                $('#AutoLevelCombo3').change(function (e) {
                    SuperLevel.val($('#AutoLevelCombo1').val() + " " + $('#AutoLevelCombo2').val() + " " + $('#AutoLevelCombo3').val());
                });
            })

        },

        WhoIs: function () {
        }
    }
})(jQuery);


function fn_ProductZIPDown(url, obj, q1, q2, q3, q4) {

    var pl = new JSONClientParameters();
    pl.add("Q1", q1);
    pl.add("Q2", q2);
    pl.add("Q3", q3);
    pl.add("Q4", q4);
    PostJsonData(url, pl, function (data) {
        var _data = eval("data.Records");
        var Rows = _data.length;
        var selHtml = "<option value=''>-선택-</option>";
        for (var i = 0; i < Rows; i++) {
            var value = _data[i];
            selHtml += String.format("<option value='{0}'>{1}</option>", value.CodeID, value.CodeNAME);
        }
        obj.html(selHtml);
    });

}
